package com.atguigu.guli.service.edu.controller.admin;



import com.atguigu.commonutils.result.BaseResult;
import com.atguigu.commonutils.result.PageResult;
import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.vo.TeacherQueryVo;
import com.atguigu.guli.service.edu.feign.OssFileSerrvice;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.experimental.Accessors;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wangyang
 * @since 2021-04-28
 */
@Api(tags = "教师管理")
@RestController
@Slf4j
@RequestMapping("/admin/edu/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileSerrvice ossFileSerrvice;

    @ApiOperation("查询所有教师")
    @GetMapping("/findAll")
    public R findAll() {
        log.info("输出所有教师列表");
        return R.ok().data("items",teacherService.list());
    }

    @ApiOperation("通过Id逻辑删除教师")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable String id) {
        teacherService.removeAvatarById(id);
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok().message("删除成功！");
        }else {
            return  R.error().message("删除失败！");
        }
    }


    @ApiOperation("通过条件查询教师")
    @GetMapping("/findPage/{currentPage}/{pageSize}")
    public R findPage(
            @ApiParam(value = "当前页码数",required = true) @PathVariable long currentPage,
            @ApiParam(value = "页面大小",required = true) @PathVariable long pageSize,
            @ApiParam(value = "条件查询",required = false) TeacherQueryVo teacherQueryVo
            ){
       /* Page<Teacher> pageParam = new Page<>(currentPage,pageSize);
        Page<Teacher> teacherPage = teacherService.page(pageParam);
        return new PageResult(teacherPage);*/

        Page<Teacher> teacherIPage =(Page) teacherService.selectPage(currentPage, pageSize, teacherQueryVo);
        long total = teacherIPage.getTotal();
        List<Teacher> list = teacherIPage.getRecords();
        return R.ok().message("查询成功").data("total",total).data("rows",list);
    }

    @ApiOperation("增加教师")
    @PostMapping("/save")
    public R save(@ApiParam(value = "教师信息",required = true) @RequestBody Teacher teacher) {
        boolean result = teacherService.save(teacher);
        if (result) {
            return R.ok().message("添加成功！");
        }else {
            return  R.error().message("添加失败！");
        }
    }
    @ApiOperation("通过id修改教师信息")
    @PostMapping("/update")
    public R updateById(@ApiParam(value = "教师信息",required = true) @RequestBody Teacher teacher) {
        boolean result = teacherService.updateById(teacher);
        if (result) {
            return R.ok().message("修改成功！");
        }else {
            return R.error().message("数据不存在！");
        }
    }

    @ApiOperation("通过id查询教师信息")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "教师Id",required = true) @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().message("查询成功！").data("item",teacher);
        } else {
            return R.error().message("数据不存在！");
        }
    }


    @ApiOperation("通过勾选批量删除讲师")
    @DeleteMapping("/batch-remove")
    public R batchRemove(
            @ApiParam(value = "教师的id集合",required = true)
            @RequestBody List<String> idList
    ){
        boolean result = teacherService.removeByIds(idList);
        if(result) {
            return R.ok().message("批量删除成功");
        } else {
            return R.error().message("批量删除失败");
        }
    }

    @ApiOperation("测试openFeign服务调用")
    @GetMapping("/test")
    public R test() {
        ossFileSerrvice.test();
        return R.ok();
    }


    @ApiOperation("讲师姓名模糊查询")
    @GetMapping("/list/name/{key}")
    public R selectByKey(
            @ApiParam(value = "讲师名字",required = true)
            @PathVariable String key
    ){
        List<Map<String,Object>> nameList = teacherService.selectByKey(key);
        return R.ok().data("nameList",nameList);
    }

}

